/*
 * Jd_ListeAdresse.java
 *
 * Created on 6 avril 2008, 18:04
 */

package gui;

import IBO.IAdresse;
import IBO.IClient;
import IUSECASE.IUC_Factory;
import boerrors.CodePostalMinimumMultiplicityException;
import boerrors.ZoneMinimumMultiplicityException;
import java.rmi.RemoteException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.DefaultTableModel;
 
/**
 *
 * @author  lau
 */
public class Jd_ListeAdresseParClient extends javax.swing.JDialog {
    private DefaultTableModel _tableModel = new DefaultTableModel() {
        public boolean isCellEditable(int rowIndex, int mColIndex) {
            return false;
        }
    };
    private Frm_Application _parent;
    private int _client;
    private IXchangeAdresse _ixChangeAdresse;
    private String[] colname = {"Identifiant", "Rue","Numero","Code Postal", "Ville", "Pays", "Zone"};

    
    /** Creates new form Jd_ListeAdresse */
    public Jd_ListeAdresseParClient(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        setLocationRelativeTo(null);
    }
    
    public void initComponents2(Frm_Application parent, IXchangeAdresse adresse,IClient client) {
        try {
            this._parent = parent;
            this._ixChangeAdresse = adresse;
            this._client=client.getIdClient();
            this._tableModel.setColumnIdentifiers(this.colname);
        } catch (RemoteException ex) {
            Logger.getLogger(Jd_ListeCodePostaux.class.getName()).log(Level.SEVERE, null, ex);
        }
    }




    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel7 = new javax.swing.JPanel();
        jLabel11 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jbtn_selectionner = new javax.swing.JButton();
        jbtn_annuler = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        jPanel7.setBackground(new java.awt.Color(0, 102, 153));
        jPanel7.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel11.setFont(new java.awt.Font("Tahoma", 3, 14));
        jLabel11.setForeground(new java.awt.Color(255, 255, 255));
        jLabel11.setText("Liste des adresses du client");

        javax.swing.GroupLayout jPanel7Layout = new javax.swing.GroupLayout(jPanel7);
        jPanel7.setLayout(jPanel7Layout);
        jPanel7Layout.setHorizontalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel7Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel11)
                .addContainerGap(247, Short.MAX_VALUE))
        );
        jPanel7Layout.setVerticalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel7Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel11)
                .addContainerGap())
        );

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "Résultat"));

        jTable1.setModel(this._tableModel);
        jScrollPane1.setViewportView(jTable1);

        jbtn_selectionner.setText("Sélectionner");
        jbtn_selectionner.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtn_selectionnerActionPerformed(evt);
            }
        });

        jbtn_annuler.setText("Annuler");
        jbtn_annuler.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtn_annulerActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(266, Short.MAX_VALUE)
                .addComponent(jbtn_selectionner)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jbtn_annuler)
                .addGap(3, 3, 3))
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 435, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 313, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbtn_selectionner)
                    .addComponent(jbtn_annuler)))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel7, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jbtn_annulerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtn_annulerActionPerformed
        
        this.dispose();
        
}//GEN-LAST:event_jbtn_annulerActionPerformed

    private void jbtn_selectionnerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtn_selectionnerActionPerformed
        String[] objAdresse = new String[7];
        objAdresse[0] = (String) jTable1.getModel().getValueAt(jTable1.getSelectedRow(), 0);
        objAdresse[1] = (String) jTable1.getModel().getValueAt(jTable1.getSelectedRow(), 1);
        objAdresse[2] = (String) jTable1.getModel().getValueAt(jTable1.getSelectedRow(), 2);
        objAdresse[3] = (String) jTable1.getModel().getValueAt(jTable1.getSelectedRow(), 3);
        objAdresse[4] = (String) jTable1.getModel().getValueAt(jTable1.getSelectedRow(), 4);
        objAdresse[5] = (String) jTable1.getModel().getValueAt(jTable1.getSelectedRow(), 5);
        objAdresse[6] = (String) jTable1.getModel().getValueAt(jTable1.getSelectedRow(), 6);
        
        this._ixChangeAdresse.setAdresse(objAdresse);
        System.out.println("Selection: " + jTable1.getModel().getValueAt(jTable1.getSelectedRow(), 2));
        this.dispose();
        
}//GEN-LAST:event_jbtn_selectionnerActionPerformed

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
        
        
        String[] objTmp = new String[7];
        Vector<IAdresse> listeAdresse;
        try {
            this._tableModel.setRowCount(0);

            IUC_Factory fact = this._parent.getFactoryRmi();
            listeAdresse = fact.getGestionClients().listerAdresseParClient(this._client);//jtxt_Cp.getText());
            for(int i = 0; i < listeAdresse.size() ; i++){
                objTmp[0] = String.valueOf(listeAdresse.get(i).getIdAdresse());
                objTmp[1] = listeAdresse.get(i).getRue();
                objTmp[2] = listeAdresse.get(i).getNumero();
                objTmp[3] = listeAdresse.get(i).getICodePostal().getCodePostal();
                objTmp[4] = listeAdresse.get(i).getICodePostal().getVille();
                objTmp[5] = listeAdresse.get(i).getICodePostal().getPays();
                objTmp[6] = listeAdresse.get(i).getICodePostal().getZone().getLibelleZone();
                this._tableModel.insertRow(i, objTmp);
            }
                 
        } catch (CodePostalMinimumMultiplicityException ex) {
            Logger.getLogger(Jd_ListeAdresseParClient.class.getName()).log(Level.SEVERE, null, ex);
        } catch (RemoteException ex) {
            Logger.getLogger(Jd_ListeCodePostaux.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ZoneMinimumMultiplicityException ex) {
            Logger.getLogger(Jd_ListeCodePostaux.class.getName()).log(Level.SEVERE, null, ex);
        } 
    }//GEN-LAST:event_formWindowOpened
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                Jd_ListeCodePostaux dialog = new Jd_ListeCodePostaux(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel11;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JButton jbtn_annuler;
    private javax.swing.JButton jbtn_selectionner;
    // End of variables declaration//GEN-END:variables
    
}
